package com.utils.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.TreeMap;

/**
 * @author Devin
 * @program my-java
 * @description
 * @date 2024-11-11 12:12
 **/
public class UnitTest2 {
    public static void main(String[] args) {
//        int startInt = getStartDate("2024-11-10 00:00:00");
//        System.out.println(daysBetween(startInt));

//        for (int i = 3; i > 0; i--) {
//            System.out.println(i);
//        }

//        TreeMap<Integer, Integer> map = new TreeMap<>();
//        map.put(3,100);
//        map.put(30,222);
//        map.put(100,222);
////        System.out.println(map.floorKey(1));
//        System.out.println(getFloorKeyOrMin(map, 1));
//        System.out.println(getFloorKeyOrMin(map, 122));
//        System.out.println(getFloorKeyOrMin(map, 30));

//        ArrayList<Integer> adventureList = new ArrayList<>();
//        adventureList.add(2);
//        adventureList.add(2);
//        int erciNum = (int) adventureList.stream().filter(JYValue -> JYValue.equals(2)).count();
//        System.out.println(erciNum);

        System.out.println((1733647468 - 1733645925) / 86400);
    }

    public static  <K extends Comparable<K>, V> K getFloorKeyOrMin(TreeMap<K, V> map, K key) {
        // 获取小于或等于给定 key 的最大键
        K floorKey = map.floorKey(key);

        if (floorKey == null) {
            // 如果没有小于等于 key 的键，返回最小的键
            return map.firstKey();
        }
        if(floorKey.equals(key)){
            return floorKey;
        }

        return map.higherKey(floorKey);
    }

    public static int getStartDate(String startDate) {
        startDate.replace("/", "-");
        int startTime = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(startDate);
            startTime = (int) (date.getTime() / 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return startTime;
    }

    private static long daysBetween(long timestamp) {
        // 将秒级时间戳转换为 LocalDate
        LocalDate dateFromTimestamp = Instant.ofEpochSecond(timestamp)
                .atZone(ZoneId.systemDefault())
                .toLocalDate();

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 计算相距的天数
        long daysDifference = ChronoUnit.DAYS.between(dateFromTimestamp, currentDate);

        // TODO 如果是同一天，返回 1?；否则返回实际天数
        return daysDifference +1;
    }
}
